<!-- shipCertificateSee -->

<template>
  <div class="loadHomeWork">
    <div class="loadHomeWorkIneer">
      <div class="loadTop">其他规范</div>
    </div>
  </div>
</template>

<script>
import EventBus from "@/mitt/eventBus.js";
import rename from "@/mitt/getPath.js";
import service from "@/api/api";
import $ from "jquery";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import axios from "axios";
// import userName from "@/assets/img/userName.png";
export default {
  name: "",
  data() {
    return {
      shipPageNumer: 1,
      multi: [],
      totalNumber: 0,
      locale: zhCn,
      token: "",
      tableData: [],
      tableWidth: null,
      page: 1,
      size: 20,
      pageList: [],
      show: "none",
      addOrEdit: "新增",
      currentNum: 0,
      name: "",
      number: "",
      format: "",
      file: "",
      issueDate: "",
      visaDate: "",
      limitedTerm: "",
      issuingUnit: "",
      id: "",
      totalPages: null,
      uploadfile: true,
      data: {
        name: "",
        number: "",
        format: "",
        file: "",
        issueDate: "",
        visaDate: "",
        limitedTerm: "",
        issuingUnit: "",
        id: "",
      },
      show1: "none",
      color: "#EBEBEB",
      color1: "#EBEBEB",
      file: null,
      fileName: "",
      jtData: null,
      uploadfile1: "none",

      fill1: null,
      fileName1: "",
      upFileList: [],
      upFileListName: [],
      showFile: "none",
      changeFileText: "",
      changeFileUp: "none",
      editAddNewFIle: false,
      endPickerOptions: {
        disabledDate: (time) => {
          // 默认情况下，结束日期不能早于开始日期
          if (this.issueDate) {
            return time.getTime() < this.issueDate.getTime();
          }
          return false;
        },
      },
    };
  },

  methods: {
    indexNumber(index) {
      return (this.page - 1) * this.size + index + 1;
    },
    // 监听第一个日期选择器的值变化
    handleStartDateChange() {
      if (
        this.issueDate &&
        this.limitedTerm &&
        this.issueDate > this.limitedTerm
      ) {
        // 如果开始日期晚于结束日期，提示用户并重置结束日期
        this.$message.warning("签发日期不能晚于有效期，已重置有效期");
        this.limitedTerm = null;
      }

      // 更新第二个日期选择器的可选范围
      this.endPickerOptions = {
        disabledDate: (time) => {
          // 结束日期不能早于开始日期
          if (this.issueDate) {
            return time.getTime() < this.issueDate.getTime();
          }
          return false;
        },
      };
    },
    // 监听第二个日期选择器的值变化
    handleEndDateChange() {
      if (
        this.issueDate &&
        this.limitedTerm &&
        this.limitedTerm < this.issueDate
      ) {
        // 如果结束日期早于开始日期，提示用户并重置结束日期
        this.$message.warning("有效期不能早于签发日期，已重置有效期");
        this.limitedTerm = null;
      }
    },
    handleRowDblclick(row) {
      this.clickEdit(row.id);
    },
    skipPage() {
      if (this.shipPageNumer <= this.totalPages && this.shipPageNumer > 0) {
        this.page = this.shipPageNumer;
        this.getRenderList();
      } else {
        this.$message({
          type: "warning",
          message: "超出页面数量范围",
        });
      }
    },
    // 批量删除开始========================
    selecttableFunction(row) {
      if (row.length > 0) {
        this.multi = [];
        row.forEach((element) => {
          this.multi.push(element.id);
        });
      } else {
        this.multi = [];
      }
    },
    deleteMulti() {
      // let that = this;
      if (this.multi.length == 0) {
        return this.$message({
          type: "warning",
          message: "请先选中再进行批量删除操作！",
        });
      }
      let data = {
        idList: this.multi + "",
      };

      this.$alert("确定删除选中的信息？", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            service({
              method: "delete",
              url: `/ship-certificate`,
              params: data,
            }).then((r) => {
              if (r.status == "SUCCESS") {
                this.$message({
                  type: "success",
                  message: "删除成功",
                });
                if (this.currentNum == this.multi.length && this.page > 1) {
                  this.page--;
                }
                this.getRenderList();
              }
            });
          } else {
            this.$message({
              type: "info",
              message: "取消删除",
            });
          }
        },
      });
    },
    //批量删除结束=============================
    // 上传pdf图片等文件开始==================================================
    clickDeleteFile(a, b) {
      let data = {
        type: 4,
        fileName: b,
      };
      let num = this.upFileListName.indexOf(b);
      this.upFileListName.splice(num, 1);
      this.upFileList.splice(num, 1);
      service({
        method: "delete",
        url: "/system/delete-file",
        params: data,
      })
        .then((e) => {
        })
        .catch((e) => {});
    },
    showXiQIng(a) {
      if (a) {
        if (JSON.parse(a).length > 0) {
          return "详情";
        } else {
          return "";
        }
      } else {
        return "";
      }
    },
    editAddnewFile() {
      this.changeFileUp = "block";
      this.editAddNewFIle = true;
    },
    // 更改文件
    changeFile(a, b) {
      this.changeFileText = b;
      this.changeFileUp = "block";
    },
    closeDialog5() {
      this.showFile = "none";
    },
    // 下载图片和pdf
    async clickDownLoadFile(a, b) {
      let that = this;
      let time = this.getFileName();
      this.token = sessionStorage.getItem("token");
      this.token = JSON.parse(this.token);

      let url = this.$loadFile + "/system/download/" + b;

      axios({
        method: "get",
        url: url,
        responseType: "blob", // 关键：设置响应类型为blob
        params: {
          type: 4,
        },
        headers: {
          token: that.token,
        },
      }).then((response) => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement("a");
        link.href = url;
        link.download = time + b;
        // b.toString();
        // if (b.includes(".png") || b.includes(".jpg")) {
        //   link.download = time + b; // 替
        // } else if (b.includes(".pdf") || b.includes(".PDF")) {
        //   link.download = time + b;
        // }
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
      });
    },
    clickSeeFile(a) {
      const link = document.createElement("a");
      // link.download = "image.png";
      link.href = a;
      link.target = "blank";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    },

    showDetileFile(a) {
      this.upFileList = [];
      this.upFileListName = [];
      this.showFile = "block";

      JSON.parse(a.file).forEach((item) => {
        this.upFileList.push({
          url: this.$link + "certificate/" + item,
          name: item,
        });
        this.upFileListName.push(item);
      });
    },

    getFile1() {
      this.file = event.target.files[0];
      this.fileName1 = this.file.name;
      document.getElementsByClassName("addfile")[0].value = "";
    },
    // 上传图片
    submitAddFile1() {
      if (this.addOrEdit == "编辑" && !this.editAddNewFIle) {
        if (
          (this.changeFileText.includes(".pdf") &&
            (!this.fileName1.includes(".pdf") ||
              !this.fileName1.includes(".PDF"))) ||
          (this.changeFileText.includes(".jpg") &&
            !this.fileName1.includes(".jpg")) ||
          (this.changeFileText.includes(".png") &&
            !this.fileName1.includes(".png"))
        ) {
          return this.$message({
            message: "不同文件类型无法替换，你可以选择新增",
          });
        }
      }
      if (this.file) {
        let data = {
          file: this.file,
          type: 4,
          fileName: this.changeFileText,
        };
        service({
          method: "post",
          url: "/system/upload-file",
          headers: {
            "Content-Type": "multipart/form-data",
          },
          data: data,
        })
          .then((res) => {
            document.getElementById("addfile").value = "";
            this.$message({
              message: "选择成功",
              type: "success",
            });
            this.file1 = "";
            this.changeFileUp = "none";
            if (this.editAddNewFIle && this.addOrEdit == "编辑") {

              this.upFileList.push({
                url: this.$link + "certificate/" + res.data,
                name: res.data,
              });
              this.upFileListName.push(res.data);

            } else if (this.addOrEdit == "新增") {
              this.upFileList.push({
                url: this.$link + "certificate/" + res.date,
                name: res.data,
              });
              this.upFileListName.push(res.data);
            }
            this.fileName1 = "";
            this.uploadfile1 = "none";
            this.changeFileText = "";
            this.editAddNewFIle = false;
          })
          .catch((err) => {
            document.getElementById("addfile").value = "";
            this.$message({
              message: err,
              type: "fail",
            });
            this.changeFileText = "";
          });
      } else {
        this.$message({
          message: "请先选择文件再上传",
          type: "warning",
        });
      }
    },

    // 上传图片PDF文件结束===================================================
    // 上传excel文件

    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property];

      if (data == null || data == "" || data == undefined) {
        return null;
      }
      let date = new Date(data);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return `${year}/${month}/${day}`;
    },
    formatincludeFlag(row, column) {
      // 获取单元格数据
      let data = row[column.property];
      if (data == null) {
        return null;
      }
      if (data == 0) {
        return "否";
      } else if (data == 1) {
        return "是";
      }
    },
    customIndex(index) {
      return (this.page - 1) * this.size + index + 1;
    },
    // 导入文件================
    uploadFile() {
      this.uploadfile = "block";
    },
    getFile(event) {
      this.file = event.target.files[0];
      this.fileName = this.file.name;

    },
    submitAddFile() {
      if (this.file) {
        let data = {
          file: this.file,
        };
        service({
          method: "post",
          url: "/ship-certificate/upload",
          headers: {
            "Content-Type": "multipart/form-data",
          },
          data: data,
        })
          .then((res) => {
            document.getElementsByClassName("addfile")[0].value = "";
            this.$message({
              message: "上传成功",
              type: "success",
            });
            this.file = "";
            this.fileName = "";
            this.uploadfile = "none";
            this.getRenderList();
          })
          .catch((err) => {
            this.$message({
              message: err,
              type: "fail",
            });
          });
      } else {
        this.$message({
          message: "请先选择文件再上传",
          type: "warning",
        });
      }
    },
    cancelUp() {
      this.file = "";
      this.fileName = "";
      this.uploadfile = "none";
      this.uploadfile1 = "none";
      this.changeFileUp = "none";
    },
    // 导入文件结束=============
    clickEdit(a) {
      this.upFileList = [];
      this.upFileListName = [];
      this.addOrEdit = "编辑";
      this.tableData.forEach((item) => {
        if (item.id == a) {
          this.name = item.name;
          this.number = item.number;
          this.format = item.format;
          this.file = item.file;

          this.issueDate = item.issueDate;
          this.visaDate = item.visaDate;
          this.limitedTerm = item.limitedTerm;
          this.issuingUnit = item.issuingUnit;
          this.id = a;
          if (item.file) {
            JSON.parse(item.file).forEach((item) => {
              this.upFileList.push({
                url: this.$link + "certificate/" + item,
                name: item,
              });
              this.upFileListName.push(item);
            });
          }
        }
        this.show = "block";
      });
    },

    // 删除数据
    deleteOne(a) {
      // let that = this;
      this.$alert("确定删除这条信息？", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            service({
              method: "delete",
              url: `/ship-certificate/${a}`,
            })
              .then((r) => {
                if (r.status == "SUCCESS") {
                  this.$message({
                    type: "success",
                    message: "删除成功",
                  });
                  if (this.currentNum == 1 && this.page > 1) {
                    this.page--;
                  }
                  this.getRenderList();
                  this.show = "none";
                }
              })
              .catch((err) => {
                this.$message({
                  type: "danger",
                  message: "出错了！",
                });
              });
          } else {
            this.$message({
              type: "info",
              message: "取消删除",
            });
          }
        },
      });
    },
    // 弹窗确认按钮
    sureAdd() {
      this.data.name = this.name;
      this.data.number = this.number;
      this.data.format = this.format;
      this.data.file = JSON.stringify(this.upFileListName);
      this.data.issueDate = this.issueDate;
      this.data.visaDate = this.visaDate;
      this.data.limitedTerm = this.limitedTerm;
      this.data.issuingUnit = this.issuingUnit;
      this.data.id = this.id;
      if (this.addOrEdit == "编辑") {
        service({
          method: "put",
          data: this.data,
          url: "/ship-certificate",
        })
          .then((r) => {
            if (r.status == "SUCCESS") {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getRenderList();
              this.show = "none";
              this.clearCanShu();
            }
          })
          .catch((err) => {
            this.$message({
              type: "info",
              message: "出错了！",
            });
          });
      } else {
        service({
          method: "post",
          data: this.data,
          url: "/ship-certificate",
        })
          .then((r) => {
            if (r.status == "SUCCESS") {
              this.$message({
                type: "success",
                message: "新增成功",
              });
              this.getRenderList();
              this.show = "none";
              this.clearCanShu();
            }
          })
          .catch((err) => {
            this.$message({
              type: "info",
              message: "出错了！",
            });
          });
      }
    },
    reduceDate(a) {
      let date = new Date(a);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return `${year}-${month}-${day}`;
    },
    // 关闭弹窗
    close() {
      this.show = "none";
      this.show1 = "none";
      this.clearCanShu();
      this.uploadfile = "none";
      this.uploadfile1 = "none";
      this.changeFileUp = "none";
      this.upFileList = [];
      this.upFileListName = [];
    },
    // 新增
    addOne() {
      this.addOrEdit = "新增";
      // this.$refs["dialog"].style.display = show;
      this.show = "block";
    },
    // 导出文件
    getFileName() {
      let date = new Date();
      let year = date.getFullYear().toString();
      let month =
        date.getMonth() + 1 > 9
          ? date.getMonth() + 1
          : "0" + (date.getMonth() + 1);
      let day =
        date.getDate() > 10 ? date.getDate().toString() : "0" + date.getDate();
      let hour =
        date.getHours() > 10
          ? date.getHours().toString()
          : "0" + date.getHours();
      let min =
        date.getMinutes() > 10
          ? date.getMinutes().toString()
          : "0" + date.getMinutes();
      date = null;
      return `${year}${month}${day}${hour}${min}`;
    },
    async getExcelFile() {
      let that = this;
      let time = this.getFileName();
      this.token = sessionStorage.getItem("token");
      this.token = JSON.parse(this.token);
      axios({
        method: "get",
        url: this.$loadFile + "/ship-certificate/export",
        responseType: "blob", // 关键：设置响应类型为blob
        data: {
          fileName: "船舶证书",
        },
        headers: {
          token: that.token,
        },
      })
        .then((response) => {
          const url = window.URL.createObjectURL(new Blob([response.data]));
          // 创建一个隐藏的<a>标签，并设置其href属性为上面创建的URL
          const link = document.createElement("a");
          link.href = url;

          link.download = "船舶证书-" + time + ".xls"; // 替
          document.body.appendChild(link);

          link.click();

          document.body.removeChild(link);

          window.URL.revokeObjectURL(url);
        })
        .catch((error) => {
          this.$message({
            message: "文件下载失败",
            type: "info",
          });
        });
    },
    sendMessage() {
      let res = rename();
      EventBus.emit("custom-event", res);
    },
    // 获取列表数据
    async getRenderList() {
      let data = {
        page: this.page,
        size: this.size,
      };

      await service({
        methods: "get",
        url: "ship-certificate/find-one-page",
        params: data,
      })
        .then((e) => {
          this.totalPages = e.data.totalPages;
          this.totalNumber = e.data.totalElements;
          if (this.totalPages == 0) {
            this.totalPages = 1;
          }
          if (this.page < this.totalPages) {
            this.color1 = "#268FDE";
          } else {
            this.color1 = "#EBEBEB";
          }
          if (this.page == 1) {
            this.color = "#EBEBEB";
          } else {
            this.color = "#268FDE";
          }
          this.tableData = e.data.content;
          this.currentNum = e.data.numberOfElements;
        })
        .catch((err) => {});
    },
    formatterDate(a) {
      let date = new Date(a);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      return `${year}/${month}/${day}`;
    },
    clearCanShu() {
      this.name = "";
      this.number = "";
      this.format = "";
      this.file = "";
      this.issueDate = "";
      this.visaDate = "";
      this.limitedTerm = "";
      this.issuingUnit = "";
      this.id = "";
      this.upFileList = [];
      this.upFileListName = [];
      this.data = {
        name: "",
        number: "",
        format: "",
        file: "",
        issueDate: "",
        visaDate: "",
        limitedTerm: "",
        issuingUnit: "",
        id: "",
      };
    },
    clickSee(a) {
      this.addOrEdit = "查看";
      this.upFileList = [];
      this.upFileListName = [];
      this.tableData.forEach((item) => {
        if (item.id == a) {
          this.name = item.name;
          this.number = item.number;
          this.format = item.format;
          this.file = item.file;
          this.issueDate = item.issueDate;
          this.visaDate = item.visaDate;
          this.limitedTerm = item.limitedTerm;
          this.issuingUnit = item.issuingUnit;
          this.id = a;
          if (item.file) {
            JSON.parse(item.file).forEach((item) => {
              this.upFileList.push({
                url: this.$link + "certificate/" + item,
                name: item,
              });
              this.upFileListName.push(item);
            });
          }
        }
        this.show1 = "block";
      });
    },
    clickLastPage() {
      if (this.page > 1) {
        this.page--;
        if (this.page == 1) {
          this.color = "#EBEBEB";
        } else {
          this.color = "#268FDE";
        }
        if (this.page < this.totalPages) {
          this.color1 = "#268FDE";
        }
        this.getRenderList();
      } else {
        this.color = "#EBEBEB";
      }
    },
    clickNextPage() {
      if (this.page < this.totalPages) {
        this.page++;
        this.color = "#268FDE";
        if (this.page == this.totalPages) {
          this.color1 = "#EBEBEB";
        } else {
          this.color1 = "#268FDE";
        }
      }
      this.getRenderList();
    },
    handSize() {
      if (window.innerWidth > 630) {
        if (this.$store.state.width == 200) {
          this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
        } else if (this.$store.state.width == 90) {
          this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
        }
      } else {
        this.tableWidth = window.innerWidth - 30 + "px";
      }
    },
  },
  watch: {
    "$store.state.width"(newVal, oldVal) {
      //对数据执行操作
      if (window.innerWidth > 630) {
        if (newVal == 90) {
          this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
        } else if (newVal == 200) {
          this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
        }
      }
    },
  },

  created() {
    this.sendMessage();
    this.getRenderList();
  },
  beforeMount() {
    if (window.innerWidth > 630) {
      if (this.$store.state.width == 200) {
        this.tableWidth = (window.innerWidth - 170) * 0.985 - 20 + "px";
      } else if (this.$store.state.width == 90) {
        this.tableWidth = (window.innerWidth - 60) * 0.985 - 20 + "px";
      }
    } else {
      this.tableWidth = window.innerWidth - 30 + "px";
    }
  },
  mounted() {
    let that = this;
    this.getRenderList();

    // this.sendMessage();
    window.addEventListener("resize", this.handSize);
  },
  beforeUnmount() {
    window.removeEventListener("resize", this.handSize);
  },
};
</script>
<style lang="less" scoped>
.addfile {
  width: 100%;
  height: 40px;
  font-size: 15px;
}
.addfile::file-selector-button {
  width: 100%;
  height: 4vh;
  background-color: #1e9fff;
  border: 1px solid #1e9fff;
  border-radius: 3px;
  cursor: pointer;
  color: #fff;
  font-size: 15px;
}
.fileName {
  font-size: 15px;
  color: black;
  height: 3vh;
  line-height: 3vh;
  width: 100%;
  text-align: center;
}

.manyInput {
  width: 780px;
  height: 80vh;
  overflow: auto;
  // background: red;
  margin: auto;

  // overflow: scroll;
  .iuptInnerContain {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .inputInner {
      width: 375px;
      height: 35px;
      // background: blue;
      margin-right: 10px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      position: relative;
      // justify-content: space-between;
      color: #365488;
      .name {
        width: 150px;
        height: 100%;
        line-height: 4vh;
        text-align: right;
        font-size: 15px;
      }

      .inputContainer {
        width: 210px;
        margin-left: 5px;
      }
      .el-input__inner {
        background: white !important;
      }
      input {
        width: 220px;
        height: 34px;
        margin-left: 5px;
        border-radius: 5px;
        text-indent: 1em;
        outline: none;
        border: none;
        background: white !important;
        color: #365488;
        border: 1px solid #dcdfe6;
      }
      input::-webkit-input-placeholder {
        color: #a8abb2;
      }
      input[type="date"] {
        // padding: 5px;
        color: #365488;
        text-align: left;
        // border: 1px solid #365488;
        border-radius: 4px;
        // background-color: #fff;
        box-sizing: border-box; /* 确保内边距和边框不会影响元素的总宽度 */
      }
      // input[type="number"] {
      //   appearance: textfield;
      //   -moz-appearance: textfield;
      // }
      // 　　input[type="number"]::-webkit-inner-spin-button,
      // 　　input[type="number"]::-webkit-outer-spin-button {
      //   -webkit-appearance: none;
      //   margin: 0;
      // }
    }
    .el-date-picker .el-picker-panel {
      background-color: white !important; /* 你想要的背景颜色 */
    }
  }
}
.dialogText {
  margin: auto;
  width: 50%;
  line-height: 4vh;
  font-size: 25px;
  color: black;
  // text-indent: 10px;
  text-align: center;
  // font-size: 20px;
  font-weight: 800;
}

.dialog,
.dialog1 {
  width: 100%;
  height: 100%;
  background: #dfe6e8;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
  z-index: 100;
}

.loadHomeWork {
  overflow: hidden;
  width: 100%;
  height: 100%;
  position: relative;
  .loadHomeWorkIneer {
    width: 98.5%;
    height: 91vh;
    margin: auto;
    margin-top: 1vh;
    background: white;

    .loadSecond {
      width: 280px;

      color: black;
      text-align: center;
      line-height: 33px;
      color: black;
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-left: 10px;
      font-size: 15px;
      .topName {
        width: 90px;
        line-height: 33px;
        background: #67c23a;
        border-radius: 5px;
        a {
          display: block;
          width: 90px;
          line-height: 33px;
          text-decoration: none;
          color: white;
        }
      }
      .topName:nth-child(3) {
        background: #242121;
      }
    }
    // .loadExcel {
    //   width: 300px;
    //   margin-top: 10px;
    //   margin-left: 10px;

    //   height: 20px;
    //   display: flex;
    //   align-items: center;
    //   justify-content: space-between;
    //   .button {
    //     width: 60px;
    //     line-height: 20px;
    //     font-size: 18px;
    //     text-align: center;
    //   }
    // }
  }
  // background: blue;
}
.table {
  width: 98.5%;
  margin: auto;
  // margin-top: 2vh;
  height: 65vh;
  // background: red;
}

@media (min-width: 0px) and (max-width: 960px) {
  .manyInput {
    width: 375px !important;
    display: block !important;
    .inputInner {
      margin: auto !important;
      // margin-top: 10px !important;
    }
  }
  .closeAndSrue {
    width: 110px;
  }
}
</style>
